package test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by Administrator on 2017/2/22.
 */
public class DynamicProxy implements InvocationHandler{
    private  InterFace impl;

    public DynamicProxy(InterFace impl){
        this.impl = impl;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("运行代理");
        return method.invoke(impl,args);
    }

    public static void main(String[] args) {
        InterFace interFace =  (InterFace)Proxy.newProxyInstance(
                InterFace.class.getClassLoader(), //代理接口加载器
                new Class[]{InterFace.class},  //该代理要实现的接口
                new DynamicProxy(new Impl())); //Handler实现
        interFace.doSomeThing("test");
    }
}
